#include "mipi_dcs.h"
#include "printf.h"
#include "i2c.h"
#include "systick.h"
#include "bsp.h"
#include <stdarg.h>
#include "lt9211.h"

#define LPT_DI 0x39
#define SPT_DI 0x15
//#define DSI_DEBUG 

void LCD_Reset(void)
{
	bsp_gpio_write_driver_rst(0);
	delay_1ms(50);

	bsp_gpio_write_driver_rst(1);
	delay_1ms(50);	
}

void writeReg(uint8_t Len, ...)
{
	va_list valist;
	int i;
	uint8_t temp;

	HDMI_WriteI2C_Byte(0xff, 0xd4);

	HDMI_WriteI2C_Byte(0x01, 0x0E);
	HDMI_WriteI2C_Byte(0x02, Len + 6);
	HDMI_WriteI2C_Byte(0x03, 0x29);
	HDMI_WriteI2C_Byte(0x03, Len);
	HDMI_WriteI2C_Byte(0x03, 0x00);

#ifdef DSI_DEBUG
	printf("init array %d :", Len);
#endif
	va_start(valist, Len);

	for (i = 0; i < Len; i++) {
		temp = va_arg(valist, int);
		HDMI_WriteI2C_Byte(0x03, temp);
#ifdef DSI_DEBUG
		printf("%02x ", temp);
#endif
	}

	va_end(valist);

#ifdef DSI_DEBUG
	printf("\r\n");
#endif

	HDMI_WriteI2C_Byte(0x01, 0x00);
}

void new_write_array(VT *vt)
{
	writeReg(4,0xFF,0x98,0x81,0x03);
	////GIP_1);
	writeReg(2,0x01,0x00);
	writeReg(2,0x02,0x00);
	writeReg(2,0x03,0x54);
	writeReg(2,0x04,0xD4);
	writeReg(2,0x05,0x00);
	writeReg(2,0x06,0x11);
	writeReg(2,0x07,0x09);
	writeReg(2,0x08,0x00);
	writeReg(2,0x09,0x00);
	writeReg(2,0x0a,0x00);
	writeReg(2,0x0b,0x00);
	writeReg(2,0x0c,0x00);
	writeReg(2,0x0d,0x00);
	writeReg(2,0x0e,0x00);
	writeReg(2,0x0f,0x26);
	writeReg(2,0x10,0x26);
	writeReg(2,0x11,0x00);
	writeReg(2,0x12,0x00);
	writeReg(2,0x13,0x00);
	writeReg(2,0x14,0x00);
	writeReg(2,0x15,0x00);
	writeReg(2,0x16,0x00);
	writeReg(2,0x17,0x00);
	writeReg(2,0x18,0x00);
	writeReg(2,0x19,0x00);
	writeReg(2,0x1a,0x00);
	writeReg(2,0x1b,0x00);
	writeReg(2,0x1c,0x00);
	writeReg(2,0x1d,0x00);
	writeReg(2,0x1e,0x40);
	writeReg(2,0x1f,0x80);
	writeReg(2,0x20,0x06);
	writeReg(2,0x21,0x01);
	writeReg(2,0x22,0x00);
	writeReg(2,0x23,0x00);
	writeReg(2,0x24,0x00);
	writeReg(2,0x25,0x00);
	writeReg(2,0x26,0x00);
	writeReg(2,0x27,0x00);
	writeReg(2,0x28,0x33);
	writeReg(2,0x29,0x33);
	writeReg(2,0x2a,0x00);
	writeReg(2,0x2b,0x00);
	writeReg(2,0x2c,0x00);
	writeReg(2,0x2d,0x00);
	writeReg(2,0x2e,0x00);
	writeReg(2,0x2f,0x00);
	writeReg(2,0x30,0x00);
	writeReg(2,0x31,0x00);
	writeReg(2,0x32,0x00);
	writeReg(2,0x33,0x00);
	writeReg(2,0x34,0x03);
	writeReg(2,0x35,0x00);
	writeReg(2,0x36,0x00);
	writeReg(2,0x37,0x00);
	writeReg(2,0x38,0x96);
	writeReg(2,0x39,0x00);
	writeReg(2,0x3a,0x00);
	writeReg(2,0x3b,0x00);
	writeReg(2,0x3c,0x00);
	writeReg(2,0x3d,0x00);
	writeReg(2,0x3e,0x00);
	writeReg(2,0x3f,0x00);
	writeReg(2,0x40,0x00);
	writeReg(2,0x41,0x00);
	writeReg(2,0x42,0x00);
	writeReg(2,0x43,0x00);
	writeReg(2,0x44,0x00);
	//GIP_2);
	writeReg(2,0x50,0x00);
	writeReg(2,0x51,0x23);
	writeReg(2,0x52,0x45);
	writeReg(2,0x53,0x67);
	writeReg(2,0x54,0x89);
	writeReg(2,0x55,0xab);
	writeReg(2,0x56,0x01);
	writeReg(2,0x57,0x23);
	writeReg(2,0x58,0x45);
	writeReg(2,0x59,0x67);
	writeReg(2,0x5a,0x89);
	writeReg(2,0x5b,0xab);
	writeReg(2,0x5c,0xcd);
	writeReg(2,0x5d,0xef);
	//GIP_3);
	writeReg(2,0x5e,0x00);
	writeReg(2,0x5f,0x0D);
	writeReg(2,0x60,0x0D);
	writeReg(2,0x61,0x0C);
	writeReg(2,0x62,0x0C);
	writeReg(2,0x63,0x0F);
	writeReg(2,0x64,0x0F);
	writeReg(2,0x65,0x0E);
	writeReg(2,0x66,0x0E);
	writeReg(2,0x67,0x08);
	writeReg(2,0x68,0x02);
	writeReg(2,0x69,0x02);
	writeReg(2,0x6a,0x02);
	writeReg(2,0x6b,0x02);
	writeReg(2,0x6c,0x02);
	writeReg(2,0x6d,0x02);
	writeReg(2,0x6e,0x02);
	writeReg(2,0x6f,0x02);
	writeReg(2,0x70,0x14);
	writeReg(2,0x71,0x15);
	writeReg(2,0x72,0x06);
	writeReg(2,0x73,0x02);
	writeReg(2,0x74,0x02);
	writeReg(2,0x75,0x0D);
	writeReg(2,0x76,0x0D);
	writeReg(2,0x77,0x0C);
	writeReg(2,0x78,0x0C);
	writeReg(2,0x79,0x0F);
	writeReg(2,0x7a,0x0F);
	writeReg(2,0x7b,0x0E);
	writeReg(2,0x7c,0x0E);
	writeReg(2,0x7d,0x08);
	writeReg(2,0x7e,0x02);
	writeReg(2,0x7f,0x02);
	writeReg(2,0x80,0x02);
	writeReg(2,0x81,0x02);
	writeReg(2,0x82,0x02);
	writeReg(2,0x83,0x02);
	writeReg(2,0x84,0x02);
	writeReg(2,0x85,0x02);
	writeReg(2,0x86,0x14);
	writeReg(2,0x87,0x15);
	writeReg(2,0x88,0x06);
	writeReg(2,0x89,0x02);
	writeReg(2,0x8A,0x02);
	//CMD_Page 4);
	writeReg(4,0xFF,0x98,0x81,0x04);
	writeReg(2,0x6E,0x3B);
	writeReg(2,0x6F,0x57);
	writeReg(2,0x3A,0x24);
	writeReg(2,0x8D,0x1F);
	writeReg(2,0x87,0xBA);
	writeReg(2,0xB2,0xD1);
	writeReg(2,0x88,0x0B);
	writeReg(2,0x38,0x01);
	writeReg(2,0x39,0x00);
	writeReg(2,0xB5,0x07);
	writeReg(2,0x31,0x75);
	writeReg(2,0x3B,0x98);
	//CMD_Page 1);
	writeReg(4,0xFF,0x98,0x81,0x01);
	writeReg(2,0x22,0x0A);
	//writeReg(2,0x25,VFP);
	//writeReg(2,0x26,VBP);
	//writeReg(2,0x27,HBP & 0xff);
	//writeReg(2,0x27,HBP >> 8);
	writeReg(2,0x25,vt->vfp);		// vfp 0..7
	writeReg(2,0x26,vt->vbp);		// vbp 0..7
	writeReg(2,0x27,(vt->hbp) & 0xff);	// hbp 0..7
	writeReg(2,0x28,vt->hbp >> 8);		// hbp 8..9
	writeReg(2,0x31,0x09);
	//writeReg(2,0x35,0x07);
	writeReg(2,0x53,0x7B);
	writeReg(2,0x55,0x40);
	writeReg(2,0x50,0x86);
	writeReg(2,0x51,0x82);
	writeReg(2,0x60,0x27);
	writeReg(2,0x62,0x20);
	writeReg(2,0xA0,0x00);
	writeReg(2,0xA1,0x12);
	writeReg(2,0xA2,0x20);
	writeReg(2,0xA3,0x13);
	writeReg(2,0xA4,0x14);
	writeReg(2,0xA5,0x27);
	writeReg(2,0xA6,0x1D);
	writeReg(2,0xA7,0x1F);
	writeReg(2,0xA8,0x7C);
	writeReg(2,0xA9,0x1D);
	writeReg(2,0xAA,0x2A);
	writeReg(2,0xAB,0x6B);
	writeReg(2,0xAC,0x1A);
	writeReg(2,0xAD,0x18);
	writeReg(2,0xAE,0x4E);
	writeReg(2,0xAF,0x24);
	writeReg(2,0xB0,0x2A);
	writeReg(2,0xB1,0x4D);
	writeReg(2,0xB2,0x5B);
	writeReg(2,0xB3,0x23);
	//Neg Register);
	writeReg(2,0xC0,0x00);
	writeReg(2,0xC1,0x13);
	writeReg(2,0xC2,0x20);
	writeReg(2,0xC3,0x12);
	writeReg(2,0xC4,0x15);
	writeReg(2,0xC5,0x28);
	writeReg(2,0xC6,0x1C);
	writeReg(2,0xC7,0x1E);
	writeReg(2,0xC8,0x7B);
	writeReg(2,0xC9,0x1E);
	writeReg(2,0xCA,0x29);
	writeReg(2,0xCB,0x6C);
	writeReg(2,0xCC,0x1A);
	writeReg(2,0xCD,0x19);
	writeReg(2,0xCE,0x4D);
	writeReg(2,0xCF,0x22);
	writeReg(2,0xD0,0x2A);
	writeReg(2,0xD1,0x4D);
	writeReg(2,0xD2,0x5B);
	writeReg(2,0xD3,0x23);
	//============ Gamma END===========);
	//writeReg(2,0xFF,0x98,0x81,0x04);
	//writeReg(2,0x2F,0x01)//bist);
	//CMD_Page 0);
	writeReg(4,0xFF,0x98,0x81,0x00);
	writeReg(2,0x11,0x00);
	delay_1ms(180);
	writeReg(2,0x29,0x00);
	writeReg(2,0x35,0x00);
}

void InitPanel(VT *vt)
{
	HDMI_WriteI2C_Byte(0xff, 0x81);
	HDMI_WriteI2C_Byte(0x0e, 0xef);
	HDMI_WriteI2C_Byte(0x0e, 0xff);
	HDMI_WriteI2C_Byte(0x0b, 0xfe);

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x47, 0x01);
	HDMI_WriteI2C_Byte(0x48, 0x01);

	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x20, 0x2f);
	HDMI_WriteI2C_Byte(0x21, 0x10);

	LCD_Reset();

	printf("Start initial panel\r\n");
	
	new_write_array(vt);

	printf("Finish initial panel\r\n");
}
